package 数据结构与算法.实验一;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Random;

public class FindWords {
    public static void main(String[] args){
        int m = 0;
        String [] words = new String[1000000];
        try{
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(
                            new FileInputStream("D:/txt/Words.txt")
                    )
            );
            String linestr;
            while((linestr = br.readLine()) != null){
                //System.out.println(linestr);
                words[m++] = linestr;
            }
            br.close();
        }catch (Exception e){
            System.out.println("文件操作失败");
            e.printStackTrace();
        }
        //System.out.println(m);
        Random random = new Random();
        int i = random.nextInt(104037) +1;
        System.out.println(i);
        String []st = new String[104038];
        for(int j = 0;j < 104038;j++){
            String []u = words[j].split(",");
            st[j] = u[0];
        }
        int e = 104038/2;
        int d = 2;
        while(true){
            int f = 104038/(2*d);
            int a = compare(st[i],st[e]);
            if(a == 1){
                e = e + f;
            }else if (a == -1){
                e = e - f;
            } else if (a == 0) {
                System.out.println("结果是\n"+words[e]);
                System.out.println(e);
                System.out.println(words[i]);
                System.out.println(i);
                break;
            }
            d = d * 2;
        }
    }

    public static int compare(String i,String r){
        char[] chars1 = i.toCharArray();
        char[] chars2 = r.toCharArray();
        int e = 0;
        int u;
        if(chars1.length >= chars2.length){
            u = chars2.length -2;
        }else{
            u = chars1.length -2;
        }
        for(int j = 0; j <= u;j++){
            if(chars1[j] > chars2[j]){
                e = 1;
                break;
            } else if (chars1[j] < chars2[j]) {
                e = -1;
                break;
            }else {
                e = 0;
            }
        }
        return e;
    }
}
